package com.zhan.rkforum.service;

import com.zhan.rkforum.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@SuppressWarnings("all")
public class DataServiceImpl implements DataService{

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    @Override
    public void recordUV(String ip) {
        String uvKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(new Date()));
        redisTemplate.opsForHyperLogLog().add(uvKey,ip);
    }

    @Override
    public long calculateUV(Date start, Date end) {
        if(start == null || end == null){
            throw new IllegalArgumentException("参数不能为空！");
        }
        List<String> keyList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        while (! calendar.getTime().after(end)){
            String uvKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(calendar.getTime()));
            keyList.add(uvKey);
            calendar.add(Calendar.DATE, 1);
        }

        String uvKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(start), simpleDateFormat.format(end));
        redisTemplate.opsForHyperLogLog().union(uvKey, keyList.toArray(new String[0]));
        return redisTemplate.opsForHyperLogLog().size(uvKey);
    }

    @Override
    public void recordDau(int userId) {
        String dauKey = RedisKeyUtil.getDauKey(simpleDateFormat.format(new Date()));
        redisTemplate.opsForValue().setBit(dauKey, userId, true);
    }

    @Override
    public long calculateDau(Date start, Date end) {
        if(start == null || end == null){
            throw new IllegalArgumentException("参数不能为空！");
        }

        List<byte[]> keyList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        while (! calendar.getTime().after(end)){
            String uvKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(calendar.getTime()));
            keyList.add(uvKey.getBytes(StandardCharsets.UTF_8));
            calendar.add(Calendar.DATE, 1);
        }
        return redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
                String dauKey = RedisKeyUtil.getUVKey(simpleDateFormat.format(start), simpleDateFormat.format(end));
                redisConnection.bitOp(RedisStringCommands.BitOperation.OR, dauKey.getBytes(StandardCharsets.UTF_8),  keyList.toArray(new byte[0][0]));
                return redisConnection.bitCount(dauKey.getBytes(StandardCharsets.UTF_8));
            }
        });
    }
}
